<template>
  <!-- 跟标签节点 -->
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keywords" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onsale">批量上架</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="offsale">批量下架</el-button>
        </el-form-item>
      </el-form>
    </el-col>


    <!--列表-->
    <el-table :data="dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;" height="600">
      <el-table-column type="selection" width="55" fixed>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="120" fixed>
      </el-table-column>
      <el-table-column prop="cover" label="封面" width="120" fixed>
        <template slot-scope="scope">
          <img :src="scope.row.cover" width="100" height="100"/>
        </template>
      </el-table-column>
      <el-table-column prop="saleprice" label="原价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="costprice" label="售价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="isnew" label="是否新车" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.isnew == 1" style="color: #5e7382">是</span>
          <span v-if="scope.row.isnew == 0" style="color: coral">否</span>
        </template>
      </el-table-column>
      <el-table-column prop="reigstertime" label="上牌时间" width="120" sortable>
      </el-table-column>
      <el-table-column prop="mileage" label="里程" width="80" sortable>
      </el-table-column>
      <el-table-column prop="shopname" label="店铺名称" width="180" sortable>
      </el-table-column>
      <el-table-column prop="onsaletime" label="上架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="offsaletime" label="下架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1" style="color: #5e7382">上架</span>
          <span v-if="scope.row.state == 0" style="color: coral">下架</span>
        </template>
      </el-table-column>
      <el-table-column prop="costeffective" label="是否超值" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.costeffective == 1" style="color: #5e7382">是</span>
          <span v-if="scope.row.costeffective == 0" style="color: coral">否</span>
        </template>
      </el-table-column>
      <el-table-column prop="rushsale" label="是否急售" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.rushsale == 1" style="color: #5e7382">是</span>
          <span v-if="scope.row.rushsale == 0" style="color: coral">否</span>
        </template>
      </el-table-column>
      <el-table-column prop="quasinewcar" label="是否准新车" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.quasinewcar == 1" style="color: #5e7382">是</span>
          <span v-if="scope.row.quasinewcar == 0" style="color: coral">否</span>
        </template>
      </el-table-column>
      <el-table-column prop="transitivecountry" label="是否可迁全国" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.transitivecountry == 1" style="color: #5e7382">是</span>
          <span v-if="scope.row.transitivecountry == 0" style="color: coral">否</span>
        </template>
      </el-table-column>
      <el-table-column prop="carType.name" label="类型" width="100" sortable>
      </el-table-column>
      <el-table-column prop="seller.username" label="销售顾问" width="100" sortable>
      </el-table-column>
      <el-table-column prop="auditstate" label="审核状态" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.auditstate == 1" style="color: #5e7382">审核通过</span>
          <span v-if="scope.row.auditstate == -1" style="color: coral">审核驳回</span>
        </template>
      </el-table-column>
      <el-table-column label="操作"  width="150" fixed="right">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <br/>
          <el-button style="margin-top:5px;" type="danger" size="small" @click="handleDel(scope.$index, scope.row)">上传检测报告</el-button>
          <br/>
          <el-button style="margin-top:5px;" type="danger" size="small" @click="handleDel(scope.$index, scope.row)">资源维护</el-button>
          <br/>
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">上架</el-button>
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">下架</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--分页条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :page-sizes="[5, 10, 20, 50]"
          @size-change="handleSizeChange"
          :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="新增/修改" :visible.sync="carFormVisible" :close-on-click-modal="false">
      <el-form :model="carForm" label-width="80px"  ref="carForm">
        <el-form-item label="标题">
          <el-input v-model="carForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <!--file-list 数组里面放的是当前这个组件上传成功的所有文件信息-->
        <el-form-item label="封面">
          <el-upload
              class="upload-demo"
              action="http://localhost:8081/fastDfs/upload"
              :on-remove="coverHandleRemove"
              :on-success="coverHandleSuccess"
              :file-list="coverFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="原价">
          <el-input v-model="carForm.saleprice" style="width:220px;" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价">
          <el-input v-model="carForm.costprice" style="width:220px;" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否新车">
          <el-radio-group v-model="carForm.isnew">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上牌时间">
          <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="carForm.reigstertime"
              type="date"
              placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="里程数">
          <el-input v-model="carForm.mileage" style="width:220px;" type="number" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="超值">
          <el-radio-group v-model="carForm.costeffective">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="急售">
          <el-radio-group v-model="carForm.rushsale">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="准新车">
          <el-radio-group v-model="carForm.quasinewcar">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可迁全国">
          <el-radio-group v-model="carForm.transitivecountry">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆类型">
          <el-cascader v-model="carForm.carType"
                       :options="carTypeTree"
                       :props="{
							   checkStrictly: true,
							   label:'name',
							   value:'id'
							}"
                       clearable></el-cascader>
        </el-form-item>
        <el-form-item label="基本信息">
          <!--<el-input type="textarea" v-model="carForm.carDetail.info"></el-input>-->
            <quill-editor v-model="carForm.carDetail.info" :options="quillOption">
            </quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>

</template>

<script>
import { quillEditor } from "vue-quill-editor"; // 在页面中引入富文本编辑框的组件
import 'quill/dist/quill.core.css';   // 引入样式
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import quillConfig from "../../common/js/quill-config"
export default {
  components:{
    quillEditor   // 当key和Value的名称一样的时候,可以简写  全写:  "quillEditor":quillEditor
  },
  name:"Car",
  data(){
    return {
      quillOption:quillConfig,
      query:{
        keywords:null
      },
      dataList:[],
      listLoading:false,
      sels:[],
      total:0,
      currentPage: 1,
      pageSize:10,
      title:"新增",
      carFormVisible:false, //默认关闭弹出框
      carTypeTree:[],
      addLoading:false,
      carForm: {
        id:null,
        title:"",
        cover:"",
        saleprice:0,
        costprice:0,
        isnew:0,
        reigstertime:"",
        mileage:null,
        costeffective:0,
        rushsale:0,
        quasinewcar:0,
        transitivecountry:0,
        carType:{
          id:null,
          name:""
        },
        typeId:null,
        carDetail:{
          info:""
        }
      },
      carFormRules: {
        username: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ]
      },
      coverFileList:[]
    }
  },
  methods:{
    onsale(){
      // 获取当前被选中数据的id数组
      // this.sels 里面装的是当前选中的所有的行的数据
      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾上架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认上架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/onsale',ids).then((res) => {
          this.listLoading = false;

          if(res.data.success){
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
          }else{
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
          this.loadListData();
        });
      }).catch(() => {

      });
    },
    offsale(){
      // 取当前所有选中行的ID
      var ids = this.sels.map(item => item.id);
      //获取选中的行
      if(!this.sels || this.sels.length  <1){
        this.$message({ message: '老铁，你不选中数据，臣妾下架不了啊....',type: 'error'});
        return;
      }
      this.$confirm('确认下架选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.post('/car/offsale',ids).then((res) => {
          this.listLoading = false;

          if(res.data.success){
            this.$message({
              message: '下架成功',
              type: 'success'
            });
          }else{
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
          this.loadListData();
        });
      }).catch(() => {

      });
    },

    coverHandleSuccess(response, file, fileList){
      this.carForm.cover = response.data;
    },
    coverHandleRemove(file, fileList) {
      var filePath =file.response.data;
      this.$http.delete("/fastDfs/delete?path="+filePath)
          .then(res=>{
            if(res.data.success){
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
              this.carForm.cover = "";
            }else{
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
            }
          })
    },
    batchRemove(){
      //触发批量删除操作
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        //let para = { ids: ids };
        this.$http.patch("/car",ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.loadListData();
        });
      }).catch(() => {

      });

    },
    //提交弹出框的效果
    save(){
      this.$refs.carForm.validate((valid) => {
        if (valid) {//通过表单验证
          this.$confirm('确认提交数据吗？', '提示', {}).then(() => {
            //确认框 用户点击了确认
            this.addLoading = true;
            // 单独处理车辆的类型,因为车辆类型是一个级联下拉框,选择了以后是一个数组,但是后端carType是一个对象
            // 车辆类型的处理方式,两种
            // 方式一: 将carType由数组转为对象
            // 方式二: 将carType的值赋值给typeId,但是carType设置为null/{}  采纳这个方案
            let carType = this.carForm.carType; // []
            // 将carType数组中的最后一个值赋值给this.carForm.typeId
            if(carType){
              this.carForm.typeId = carType[carType.length-1];
              this.carForm.carType = null;
            }

            this.$http.put("/car",this.carForm).then((res) => {

              if(res.data.success){
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
              }else {
                this.$message.error(res.data.msg);
              }
              //关闭弹出框
              this.carFormVisible=false;
              this.loadListData();
              this.addLoading = false;

            });
          });
        }
      });



    },
    search(){
      this.currentPage=1;
      this.loadListData();
    },
    handleAdd(){
      this.coverFileList = [];
      // 1. 给CarForm设置默认值
      this.carForm= {
            id:null,
            title:"",
            cover:"",
            saleprice:0,
            costprice:0,
            isnew:0,
            reigstertime:"",
            mileage:null,
            costeffective:0,
            rushsale:0,
            quasinewcar:0,
            transitivecountry:0,
            carType:{
              id:null,
              name:""
            },
            typeId:null,
            carDetail:{
              info:""
            }
      };

      //2.打开弹出框
      this.carFormVisible=true;
      // 调用后端接口,查询车辆类型树,渲染级联下拉框
      this.$http.get("/carType/tree")
          .then(result => {
              result = result.data;
              if(result.success){
                  this.carTypeTree = result.data;
              }
          })
    },
    selsChange(sels){
      this.sels=sels;
    },
    //性别显示转换
    formatState: function (row, column) {
      //console.debug(row)
      return row.state == 1 ? '启用' : '废弃';
    },
    handleEdit(index,row){
      //1.做数据的回显
      Object.assign(this.carForm,row);
      //2.打开弹出框
      this.carFormVisible=true;
    },
    handleDel(index,row){
      this.$confirm('确认删除数据吗？', '提示', {
        type: 'warning'
      }).then(() => {
        //确认框 用户点击了确认
        this.$http.delete("/car/"+row.id).then((res) => {

          if(res.data.success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error(res.data.message);
          }
          this.loadListData();
        });
      });
    },
    loadListData(){
      //加载列表数据
      this.listLoading=true
      //封装请求数据
      let query ={
        currentPage:this.currentPage,
        pageSize:this.pageSize,
        keywords:this.query.keywords
      }
      //发送请求获取列表数据
      this.$http.post("/car",query).then(res=>{
        res = res.data;
        this.dataList=res.data.rows;
        this.total=res.data.count;
        //关闭加载效果
        this.listLoading=false;
      })
    },
    handleCurrentChange(num){
      this.currentPage=num;
      this.loadListData();
    },
    handleSizeChange(num){//当用户切换每页展示的条数的时候触发的方法
      this.pageSize=num;
      this.loadListData();
    },
    loadDepts(){
      //发送请求获取所有的员工
      this.$http.get("/dept").then(res=>{
        this.depts=res.data;
      })

    },
    loadDeptTree(){//加载部门树
      this.$http.get("/dept/tree").then(res=>{
        this.deptTree=res.data;

      })
    }

  },
  mounted() {
    this.loadListData();
  }

}

</script>

<style scoped>

</style>
